package com.baby.movie.app.ui;

import java.util.List;

import org.apache.http.Header;

import android.content.Context;
import android.support.v7.app.ActionBar.LayoutParams;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

import com.baby.movie.R;
import com.baby.movie.model.Video;
import com.baby.movie.model.response.VideosResponse;
import com.baby.movie.service.MTService;
import com.baby.movie.util.AppUtil;
import com.common.util.AndroidUtil;
import com.common.util.JsonUtil;
import com.common.util.Logs;
import com.common.view.MyArrayAdapter;
import com.common.view.ViewHolder;
import com.loopj.android.http.RequestHandle;
import com.loopj.android.http.TextHttpResponseHandler;

public class SearchActivity extends BaseActivity {
	
	private static final int START_PAGE = 1;

	private ListView mListView;
	
	private EditText mEtSearchKey;
	
	private View mBtnSearch;
	
	private MTAdapter mAdapter;
	
	private int mPage = START_PAGE;
	
	private int mCategoryId;
	
	@Override
	protected boolean showHome() {
		return true;
	}

	@Override
	protected int getContentViewRes() {
		return R.layout.activity_search;
	}


	@Override
	protected void findViews() {
		mListView = (ListView) findViewById(R.id.mListView);
	}


	@Override
	protected void initialize() {
		mActionBar.setDisplayShowCustomEnabled(true);
		View actionView = getLayoutInflater().inflate(R.layout.layout_search, null);
		mEtSearchKey = (EditText) actionView.findViewById(R.id.etSearchKey);
		mBtnSearch = actionView.findViewById(R.id.btnSearch);
		
		LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
		mActionBar.setCustomView(actionView, layoutParams);
		
		mBtnSearch.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				search();
			}
		});
		
		mEtSearchKey.setOnEditorActionListener(new OnEditorActionListener() {
			
			@Override
			public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
				search();
				return true;
			}
		});
		
		mAdapter = new MTAdapter(this);
		mListView.setAdapter(mAdapter);
		
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position,
					long arg3) {
				Video video = mAdapter.getItem(position);
				showMtDetail(video);
			}
		});
	}
	
	private void showMtDetail(Video video) {
		AppUtil.showMtDetail(this, video);
	}
	
	
	private class MTAdapter extends MyArrayAdapter<Video> {

		public MTAdapter(Context context) {
			super(context);
		}

		@Override
		public int getItemViewResource() {
			return R.layout.item_search;
		}

		@Override
		public ViewHolder<Video> getViewHolder(View convertView) {
			return new MTHolder(convertView);
		}
		
	}
	
	private static class MTHolder extends ViewHolder<Video> {
		
		private TextView tvTitle;

		public MTHolder(View convertView) {
			super(convertView);
			tvTitle = (TextView) convertView.findViewById(R.id.tvTitle);
		}

		@Override
		public void setData(Video d) {
			tvTitle.setText(d.Title);
		}
	}
	
	private RequestHandle mRequestHandle;
	private void search() {
		mAdapter.clear();
		
		String key = mEtSearchKey.getText().toString();
		if (TextUtils.isEmpty(key)) {
			return;
		}
		search(key, START_PAGE);
	}
	
	private void search(String q, final int page) {
		TextHttpResponseHandler responseHandler = new TextHttpResponseHandler() {
			
			@Override
			public void onSuccess(int statusCode, Header[] headers,
					String responseString) {
				Logs.v(responseString);
				try {
					VideosResponse response = JsonUtil.fromJson(responseString, VideosResponse.class);
					List<Video> data = response.Data;
					if (!AndroidUtil.isEmpty(data)) {
						mAdapter.addAll(data);
						
						mAdapter.notifyDataSetChanged();
						mPage = page + 1;
						if (mAdapter.getCount() >= response.Total) {
							// TODO all
						}
					}
				} catch (Exception e) {
					toast("Fail");
				}
			}
			
			@Override
			public void onFailure(int statusCode, Header[] headers,
					String responseString, Throwable throwable) {
				toast("找不到相关视频");
			}
		};
		
		mRequestHandle = MTService.search(page, mCategoryId, q, responseHandler);
	}
	
	@Override
	protected void onDestroy() {
		MTService.cancel(mRequestHandle);
		super.onDestroy();
	}

}
